package com.hbun.cxcy.controller;


import com.hbun.cxcy.common.utils.PageUtils;
import com.hbun.cxcy.common.utils.R;
import com.hbun.cxcy.entity.Activity;
import com.hbun.cxcy.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mahaunyu
 * @since 2022-01-24
 */
@CrossOrigin
@RestController
@RequestMapping("/cxcy/activity")
public class ActivityController {

    @Autowired
    ActivityService activityService;

    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = activityService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{activityId}")
    //@RequiresPermissions("cxcyweb:activity:info")
    public R info(@PathVariable("activityId") Long activityId) {
        Activity activity = activityService.getById(activityId);
        // 自定义活动信息
//        Activity activity = activityService.getByIdWithCustom(activityId);
        if (activity != null) {
            return R.ok().put("activity", activity);
        } else {
            return R.error();
        }

    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("cxcyweb:activity:save")
    public R save(@RequestBody Activity activity) {
        activityService.saveWithCustom(activity);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("cxcyweb:activity:update")
    public R update(@RequestBody Activity activity) {
        activityService.updateByIdWithCustom(activity);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("cxcyweb:activity:delete")
    public R delete(@RequestBody Long[] activityIds) {
        activityService.removeByIds(Arrays.asList(activityIds));

        return R.ok();
    }
}

